<?php

class DbtestModel extends DAO_MySql {

    /**
     * 要操作的表名
     *
     * @var string
     */
    private static $_table = 'test_1';

    /**
     * 主键
     *
     * @var string
     */
    private static $_pk = 'id';

    /**
     * 允许的字段
     *
     * @var array
     */
    private static $_allowed = array(
        'id',
        'data',
    );

    /**
     * 必须的字段
     *
     * @var array
     */
    private static $_required = array();

    private static $_json = array('data');

    /**
     * add
     * 添加一条记录
     *
     */
    public static function addData($data, $shardKey = array()) {

        foreach (self::$_json as $field) {
            if (isset($data[$field])) {
                $data[$field] = json_encode($data[$field]);
            }
        }

        return parent::add(self::$_table, $data, self::$_required, self::$_allowed, $shardKey);
    }

    /**
     * remove
     * 删除一条记录
     *
     */
    public static function removeData($sId, $shardKey = array()) {

        return parent::remove(self::$_table, self::$_pk, $sId, $shardKey);
    }

    /**
     * update
     * 更新一条记录
     *
     */
    public static function updateData($id, $data, $shardKey = array()) {

        if ($data[self::$_pk]) {
            unset($data[self::$_pk]);
        }

        foreach (self::$_json as $field) {
            if (isset($data[$field])) {
                $data[$field] = json_encode($data[$field]);
            }
        }

        return parent::update(self::$_table, $data, self::$_pk, $id, self::$_allowed, $shardKey);
    }

    /**
     * get
     * 获取一条记录
     *
     */
    public static function getData($id, $fields = array(), $shardKey = array()) {

        if ($fields) {
            $result = array_diff($fields, self::$_allowed);
            if ($result) {
                throw new InvalidArgumentException("Invalid Field(" . join(',', $result) . ")");
            }
        } else {
            $fields = self::$_allowed;
        }

        $conditions = self::$_pk . ' = ?';
        $params = array($id);

        $data = parent::find(self::$_table, $conditions, $params, $fields, $shardKey);

        if ($data) {
            $decodeField = array_intersect($fields, self::$_json);
            if ($decodeField) {
                foreach ($decodeField as $field) {
                    $data[$field] = json_decode($data[$field], true);
                }
            }
        }

        return $data;
    }

    /**
     * listAll
     * 列出所有记录
     *
     */
    public static function listAll(&$pageOptions = array(), $fields = array(), $shardKey = array()) {

        if ($fields) {
            $result = array_diff($fields, self::$_allowed);
            if ($result) {
                throw new InvalidArgumentException("Invalid Field(" . join(',', $result) . ")");
            }
        } else {
            $fields = self::$_allowed;
        }

        $data = parent::findAll(self::$_table, null, array(), $pageOptions, $fields, array('id' => 'ASC'), $shardKey);

        $decodeField = array_intersect($fields, self::$_json);
        if ($decodeField) {
            foreach ($data as $key => $siteInfo) {
                foreach ($decodeField as $field) {
                    $data[$key][$field] = json_decode($siteInfo[$field], true);
                }
            }
        }

        return $data;
    }

    /**
     * countAll
     * 获取所有记录总数
     *
     * @param array $shardKey 分库关键字
     * @return integer
     */
    public static function countAll($shardKey = array()) {

        return parent::count(self::$_table, null, array(), $shardKey);
    }

}
